<template>
	<view>
		<!-- 分配工程经理 -->
		<u-picker title="分配维修工程经理" :show="gcVisible" :columns="adminList" keyName="name" @confirm="gcSubmit"
			closeOnClickOverlay @close="close('gc')" @cancel="close('gc')" confirmColor="#3B75FF"></u-picker>
		<!-- 上传完成照片 -->
		<u-popup :show="scVisible" @close="close('sc')" round="10" closeable>
			<view class="popup-title">上传完成照片</view>
			<view class="popup-box">
				<view class="popup-from">
					<view class="popup-label">完成照片/视频</view>
					<file-upload :typeList="['m﻿4a', 'mp3', 'zip', 'rar']" v-if="scVisible" @afterRead="afterRead"
						@delete="deletePic"></file-upload>
				</view>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{marginTop: '30rpx'}"
					@click="scSubmit"></u-button>
			</view>
		</u-popup>

	</view>
</template>

<script>
	export default {
		name: "afterSalesManagement",
		props: {
			value: {
				type: String,
				default: ''
			},
			oper_item: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				// 分配工程经理
				gcVisible: false,
				adminList: [],
				// 上传完成照片
				scVisible: false,
				formData: {
					completion_file: []
				},
			};
		},
		watch: {
			value(val) {
				console.log(val, 'xxxx');
				switch (val) {
					case 'afterSales_completeContent':
						uni.navigateTo({
							url: '/pagesC/afterSalesManagement/add/add?id=' + this.oper_item.id
						});
						this.$emit('update:value', '');
						break;
						// 分配工程经理
					case 'afterSales_engineeringManager':
						if (this.adminList.length == 0) {
							this.getAdminList()
						} else {
							this.gcVisible = true;
						}
						break;
						// 现场反馈
					case 'afterSales_feedback':
						uni.navigateTo({
							url: '/pagesC/afterSalesManagement/xcfk/xcfk?id=' + this.oper_item.id
						});
						this.$emit('update:value', '');
						break;
						// 责任判定
					case 'afterSales_judge':
						uni.navigateTo({
							url: '/pagesC/afterSalesManagement/zrpd/zrpd?id=' + this.oper_item.id
						});
						this.$emit('update:value', '');
						break;
						// 上传完成照片
					case 'afterSales_successImg':
						this.scVisible = true;
						break;
				}
			}
		},
		methods: {
			close(type) {
				switch (type) {
					case 'gc':
						this.gcVisible = false;
						break;
					case 'sc':
						this.scVisible = false;
						this.formData = {
							completion_file: []
						}
						break;
				}
				this.$emit('update:value', '');
			},
			/********** 分配工程经理 **********/
			// 获取工程经理列表
			getAdminList() {
				this.$Apipc.getAdminList({
					page: 1,
					num: 99,
					identity: 2,
					position_id_arr: [7],
				}).then(res => {
					console.log(res, '获取工程经理列表');
					if (res.code == 200) {
						this.adminList[0] = res.data.list;
						this.gcVisible = true;
					}
				})
			},
			gcSubmit(e) {
				console.log(e, 'gcSubmit');
				uni.showLoading({
					mask: true
				});
				let oper_item = this.oper_item
				this.$hApi.selectEngineeringManager({
					after_sales_id: oper_item.id,
					engineering_manager_id: e.value[0].id,
					engineering_manager_name: e.value[0].name,
				}).then(res => {
					console.log(res, '分配工程经理');
					uni.hideLoading();
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('gc')
					}
					uni.showToast({
						title: res.code == 200 ? "操作成功" : res.message,
						icon: res.code == 200 ? "success" : "error",
					});
				})
			},


			// 上传完成照片
			afterRead(files) {
				files.map(item => {
					let obj = {
						path: item.url
					}
					this.formData.completion_file.push(obj)
				})
			},
			deletePic(e) {
				console.log(e, 'deletePic');
				this.formData.completion_file.splice(e.index, 1)
			},
			scSubmit() {
				let formData = this.formData;
				if (formData.completion_file.length == 0) {
					this.$refs.uToast.show({
						message: "完成照片/视频",
					})
					return
				}
				uni.showLoading({
					mask: true
				});
				this.$upload(formData.completion_file).then((res) => {
					console.log(res, 'zxxx');

					this.$hApi.wxAfterSalesRectification({
						after_sales_id: this.oper_item.id,
						completion_file: res.arrResult
					}).then(res => {
						console.log(res, 'wxAfterSalesRectification');
						uni.hideLoading();
						if (res.code == 200) {
							this.$emit('submit', this.value);
							this.close('sc')
						}
						uni.showToast({
							title: res.code == 200 ? "操作成功" : res.message,
							icon: res.code == 200 ? "success" : "error",
						});
					})
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.popup-title {
		height: 46px;
		border-bottom: 2rpx solid #efefef;
		text-align: center;
		line-height: 46px;
		font-size: 30rpx;
	}

	.popup-box {
		padding: 30rpx 20rpx 20rpx;

		.popup-from {
			padding: 0 20rpx;

			.popup-label {
				color: #303133;
				font-size: 15px;
				margin-bottom: 22rpx;
				position: relative;

				&::after {
					content: '*';
					position: absolute;
					left: -9px;
					color: #f56c6c;
					line-height: 12px;
					font-size: 12px;
					top: 3px;
				}
			}
		}

	}
</style>